/*
 	PureMVC UnifiedLoader Utility
 	Copyright (c) 2008 neil manuell
  	Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
 package org.puremvc.as3.multicore.utilities.unifiedloader.clients
{
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.utilities.unifiedloader.errors.OverrideMethodError;
	import org.puremvc.as3.multicore.utilities.unifiedloader.interfaces.ILoaderClientInfo;
	
	
	/**
	 * 
	 * This is an abstract class for the LoaderClients, to create a new client, it must extend this class.
	 * 
	 */
	public class LoaderClientBase
	{
		private var mLoadInfo:ILoaderClientInfo;
		
		public function LoaderClientBase(cli:ILoaderClientInfo)
		{
			mLoadInfo = cli;
		}
		/**
		 * Prepares the load process, and controls the order of sub-methods
		 * overridden by children. Called by the Loader when the LoaderRequest
		 * has reached the front of the load queue.
		 */		
		public final function load():void
		{
			initialiseLoad();
			addListeners();
			startLoad();
		}
		/**
		 * Hook into the load process - must be overridden. 
		 * Creates and configures the native loader.
		 */
		protected function initialiseLoad():void
		{
			throw new OverrideMethodError()
		}
		/**
		 * Hook into the load process - must be overridden. 
		 * Adds all necessary listeners. 
		 */
		protected function addListeners():void{
			throw new OverrideMethodError()
		}
		/**
		 * Hook into the load process - must be overridden. 
		 * Calls the native loader's load method or equivalent. 
		 * 
		 */
		protected function startLoad():void
		{
			throw new OverrideMethodError()
		}
		
		/**
		 * Prepares the asset after load complete, and controls the order of sub-methods
		 * overridden by children. This must be called from the child class when the data
		 * being retrieved is ready to be accessed, usually done by registering it as the handler
		 * for the complete event listener.
		 */	
		protected final function loadCompleteHandler(event:Event):void
		{
			removeListeners();
			prepareAsset(event);
			completedCallBack()
			relayHandler(event);
		}
		
		/**
		 * Hook into the load complete process - must be overridden. 
		 * Removes all listeners. 
		 */
		protected function removeListeners():void{

			throw new OverrideMethodError()

		}
		
		/**
		 * Hook into the load complete process - must be overridden. 
		 * Prepares the asset, wraps it, and injects it into the LoaderRequest 
		 */
		protected function prepareAsset(event:Event):void{

			throw new OverrideMethodError()

		}
		/**
		 * Handles any errors. Any error event listeners must regisiter
		 * this method as its handler.
		 */
		protected final function errorHandler(event:Event):void{
			
			removeListeners();
			handleError(event);
			
		}
		/**
		 * Hook for error handling. 
		 */
		protected function handleError(event:Event):void
		{
			relayHandler(event);
		}
		
		/**
		 *  Relays the error through the LoaderRequest
		 */
		protected final function relayHandler(event:Event):void{
			
			
			mLoadInfo.relayEvent(event);
			
		}
		
		/**
		 *  Informs the Loader that loading is complete and the asset is ready
		 */
		protected final function completedCallBack():void{
			
			mLoadInfo.completedCallBack();
			
		}

		/**
		 *  Types the LoaderRequest to the required interface
		 */
		protected final function get loaderClientInfo():ILoaderClientInfo
		{ 
			return mLoadInfo
		}

	}
}